//
//  QDMetroCodeManager-Swift.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/10.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation


/// 二维码的异常状态
enum QRCodeStatusType: String {
    /// 成功
    case success = "0000"
    /// 黑名单
    case blackList = "8005"
    /// 没有签约
    case noSign = "8011"
    /// 找不到相关签约记录
    case noPayment = "9999"
    /// 没有默认的支付渠道
    case noDefaultPayment = "7005"
}

extension QRCodeStatusType {
    
    /// 对应的header类型
    var headerErrorType: QDMetroHeaderErrorType {
        get {
            switch self {
            case .blackList: return .inBlacklist
            case .noSign,.noPayment: return .outPayType
            case .noDefaultPayment: return .outDefaultChannel
            default: return .none
            }
        }
    }
}


extension QDMetroCodeManager {
    
    /// 请求地铁乘车二维码
    ///
    /// - Parameters:
    ///   - channelType: 付款方式
    ///   - force: 是否手动刷新
    ///   - complete: 请求成功，二维码,类型
    @objc func requestMetroCode(channelType: String,
                                force: Bool = false,
                                complete:@escaping ((String,String) ->()))
    {
        
        QDNetworkingManager.request(url: QDTicketHostUrl + "/postpaid/request_ticket", paramteres: ["paymentType":channelType,"force": force, "platform_t": "ios"], success: { (msg, data) in
            
            guard let retCode = data["retCode"] as? String,
                let success = QRCodeStatusType(rawValue: retCode) else {
                self.codeDelegate?.codeManager(self, fail: .netFail); return
            }
            
            let qrcode = data["qrCode"] as? String ?? ""
            let type = data["type"] as? String ?? ""
            
            if success == .success {
                complete(qrcode,type); return
            }

            self.codeDelegate?.codeManager(self, fail:  success.headerErrorType)
            
        }) { (msg, errorType) in
            
            self.codeDelegate?.codeManager(self, fail: .netFail)
        }
    }
    
    
    /// 上送过闸消息
    @objc func updateOrderNotifyMessage(qrCode: String, agm: String){
        
        print("开始上送数据啦!");
        // 上送即可
        QDNetworkingManager.request(url: QDTicketHostUrl + "/order/notify_message", paramteres: ["agmData" : agm,"ticketData" : qrCode,"checked" : 1])
    }
}
